<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
		var aJaxURL = "server-side/pos/pos.action.php";		//server side folder url
		var seoyURL = "server-side/seoy/seoy.action.php";	    //server side folder url
		var tName 	= "pos-table";										//table name
		var fName 	= "pos-form";										//form name	
		var menuLength = [[100], [100]];
		var dialogButton = {
	        "save": {
	            text: "არჩევა",
	            id: "save-pos-select",
	            click: function () {
	            }
	        }
	    };
		var oID		= 0;
		
		$(document).ready(function (){			
			SelectPOS();
			GetDialog("pos-select-form", 400, "auto", dialogButton);

			SeoY("seoy-input", seoyURL, "pos", "", 1) ;
			
			$("#navigate").hide();
			$("#pos-wrapper").hide();
			
			$("#seoy_button").button({
	            icons: {
	                primary: "ui-icon-plusthick"
	            }
        	});
												
			$("#save_trans_button").button({
	            icons: {
	                primary: "ui-icon-disk"
	            }
        	});
        	
			$("#save_button").button({
	            icons: {
	                primary: "ui-icon-disk"
	            }
        	});
        	
			$("#cancel_button").button({
	            icons: {
	                primary: "ui-icon-closethick"
	            }
        	});
        	    	
			$("#add_payment").button({
	            icons: {
	                primary: "ui-icon-check"
	            }
        	});
		});
		
		function SelectPOS(){
			$.ajax({
				url: aJaxURL,
				data: "act=select_pos",        
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                        	$("#pos-select-form").html(data.page);
                        }
                    }
				}
			});
		}
		
		function LoadDialog(){		
		    $("#" + fName).dialog({
		        resizable:     false,
		        draggable:     false,		       
		        width:         1000,
		        height:        700,
		        modal:         false,
		        stack:         true,
                sticky:        false,
                closeOnEscape: false,
                dialogClass:   "pos-dialog"
		    });
		}
		
		function LoadTable(){
			/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
			GetDataTable(tName, aJaxURL, "get_list", 6, "obj_id=" + oID, 0, menuLength, 0, "asc");
		}
		
		$(document).on("click", "#save-pos-select", function() {
			oID		= $("#obj_id :selected").val();
			oName	= $("#obj_id :selected").text();
			$("#" + fName).attr("title", oName);
			
			LoadDialog();
			LoadTable();
			
			GetQuantity(oID);
			GetSumPrice(oID);
			
			$("#pos-wrapper").fadeIn("slow");
			$("#seoy-input").focus();
			$("#pos-select-form").dialog("close");
		});

		$(document).on("click", "#seoy_button", function() {
			param = new Object();
			
            //Action
            param.act	= "add_prod";
            
            param.name		= $("#seoy-input").val();
            param.obj_id	= oID;
            param.price		= $("#sale_price").val();
			
			$.ajax({
				url: aJaxURL,
				data: param,
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
    						GetQuantity(param.obj_id);
    						GetSumPrice(param.obj_id);
    						LoadTable();
    						$("#sale_price").val(0);
    						$("#seoy-input").val("");
                        }
                    }
				}
			});
		});
		
		$(document).on("click", "#save_button", function() {
			var obj_id	= oID;
			
			$.ajax({
				url: aJaxURL,
				data: "act=save_transaction&obj_id=" + obj_id,
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
    						GetQuantity(obj_id);
    						GetSumPrice(obj_id);
    						LoadTable();
    						alert("ტრანზაქცია შენახულია!");
                        }
                    }
				}
			});
		});

		$(document).on("click", "#cancel_button", function() {
			var obj_id	= oID;
			
			$.ajax({
				url: aJaxURL,
				data: "act=abort_transaction&obj_id=" + obj_id,        
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
    						GetQuantity(obj_id);
    						GetSumPrice(obj_id);						
    						LoadTable();
    						alert("ტრანზაქცია წარმატებით გაუქმდა!");
                        }
                    }
				}
			});
		});

		$(document).on("click", ".pos-prod-delete", function() {
			var trans_id  = $(this).text();
			var obj_id	= oID;
			
			$.ajax({
				url: aJaxURL,
				data: "act=delete_prod&trans_id=" + trans_id + "&obj_id=" + obj_id,        
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
    						GetQuantity(obj_id);
    						GetSumPrice(obj_id);
    						LoadTable();
                        }
                    }
				}
			});
		});

		$(document).on("click", "#add_payment", function() {		
			var sum			= $("#sum-price").text();
			var total_sum	= $("#sum-total-price").val();

			if(total_sum < sum){
				alert("თანხა არასაკმარისია!\nშეასწორეთ თანხა ან დაარეგისტრირეთ კლიენტი პარტნიორების სიაში!");
			}else {		
				if(confirm("დარწმუნებული ხართ ?")){
					var obj_id	= oID;
					
					$.ajax({
						url: aJaxURL,
						data: "act=payment_done&obj_id=" + obj_id,        
						success: function(data) {
							if (typeof (data.error) != "undefined") {
		                        if (data.error != "") {
		                            alert(data.error);
		                        } else {
									GetQuantity(obj_id);
									GetSumPrice(obj_id);
									LoadTable();
									$("#sale_price").val(0);
									$("#seoy-input").val("");
									
									if(total_sum > sum){
										var tsum = total_sum - sum;
										alert("ოპერაცია წარმატებით დასრულდა!\n\nხურდა: " + tsum.toFixed(2) + " GEL");
									}
		                        }
		                    }
						}
					});
		        }
			}
		});

		$(document).on("keypress", "#seoy-input", function() {
			if ( event.which == 13 ) {
				$("#seoy_button").click();
				$("#seoy-input").autocomplete("close");
			}
		});

		$(document).on("click", "#save_trans_button", function() {	
			var obj_id	= oID;
						
			$.ajax({
				url: aJaxURL,
				data: "act=suspended_page&obj_id=" + obj_id,       
				success: function(data) {
					if (data.error != "") {
                        alert(data.error);
                    } else {
	                	$("#pos-transactions-form").html(data.page);
	          			$( ".restore_suspend_button" ).button({
	        			      icons: {
	        			        primary: "ui-icon-arrowreturnthick-1-s"
	        			      }
	        			});
	          			$( ".cancel_suspend_button" ).button({
	        			      icons: {
	        			        primary: "ui-icon-trash"
	        			      }
	        			});
                    }
				}
			});
			
			var button = {
		        "cancel": {
		            text: "დახურვა",
		            id: "cancel-dialog",
		            click: function () {
		                $(this).dialog("close");
		            }
		        }
		    };
		    
			GetDialog("pos-transactions-form", 600, "auto", button);
		});
		
		$(document).on("click", ".restore_suspend_button", function() {
			var obj_id	= oID;
			var trans_id = $(this).attr("trans");
			
			$.ajax({
				url: aJaxURL,
				data: "act=restore_suspended&obj_id=" + obj_id + "&trans_id=" + trans_id,       
				success: function(data) {
					if (data.error != "") {
                        alert(data.error);
                    } else {
						GetQuantity(obj_id);
						GetSumPrice(obj_id);
						LoadTable();
	                	$("#pos-transactions-form").dialog("close");
                    }
				}
			});
		});
		
		$(document).on("click", ".cancel_suspend_button", function() {
			var obj_id	= oID;
			var trans_id = $(this).attr("trans");

			if(confirm("დარწმუნებული ხართ რომ გსურთ ტრანზაქციის წაშლა?")){
				$.ajax({
					url: aJaxURL,
					data: "act=cancel_suspend&trans_id=" + trans_id,       
					success: function(data) {
						if (data.error != "") {
	                        alert(data.error);
						} else {
		                	$("#pos-transactions-form").dialog("close");
	                    }
					}
				});
			}
		});
		
		function GetQuantity(obj_id){
			$.ajax({
				url: aJaxURL,
				data: "act=get_prod_count&obj_id=" + obj_id,        
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            if(empty(data.prod_count))
                        		$("#prod-quantity").text(0);
                            else
                        		$("#prod-quantity").text(data.prod_count);
                        }
                    }
				}
			});
		}		
				
		function GetSumPrice(obj_id){
			$.ajax({
				url: aJaxURL,
				data: "act=get_sum_price&obj_id=" + obj_id,        
				success: function(data) {
					if (typeof (data.error) != "undefined") {
                        if (data.error != "") {
                            alert(data.error);
                        } else {
                            if(empty(data.sum_price)){
        						$("#sum-price").text("00.00");
        						$("#sum-total-price").val("00.00");
                            } else{
        						$("#sum-price").text(data.sum_price);
        						$("#sum-total-price").val(data.sum_price);
                            }
                        }
                    }
				}
			});
		}		
	</script>
	
	<style type="text/css">
		.dataTable_buttons{
			display: none;
		}
	</style>
</head>
<body>
	<div id="pos-wrapper">
	    <!-- jQuery Dialog -->
	    <div id="pos-select-form" class="form-dialog" title="სალაროს არჩევა">
	    </div>
	    <!-- jQuery Dialog -->
	    <div id="pos-transactions-form" class="form-dialog" title="შეჩერებული ტრანზაქციები">
	    </div>
	    <!-- jQuery Dialog -->
	    <div id="pos-form">
	    	<table id="pos-container-table">
	    		<tr id="pos-table-header">
					<th colspan="3">
						<div>
							<div id="pos_title">
								<span>გაყიდვის რეგისტრაცია</span>
								<div id="sale_type" class="ui-widget-header ui-corner-all">
									<samp>ფასი</samp>
									<select id="sale_price">
										<option value="0">საცალო</option>
										<option value="1">საბითუმო</option>
									</select>
								</div>
							</div>
							<div id="pos_buttons" class="ui-widget-header ui-corner-all">
								<button id="save_trans_button">ტრანზაქციები</button>
								<button id="save_button">შენახვა</button>
								<button id="cancel_button">გაუქმება</button>
							</div>
						</div>
					</th>
				</tr>
				<tr>
					<td style="width: 730px">
						<div id="product-wrapper" class="fg-toolbar ui-toolbar ui-widget-header ui-corner-tl ui-corner-tr ui-helper-clearfix">
							<div id="pos-seoy">
					           	<input id="seoy-input" type="text" name="seoy-input">
					           	<button id="seoy_button">დამატება</button>
							</div>
						
							 <div class="pos-inner-table">
							    <div id="dt_example" class="ex_highlight_row">
						            <div id="dynamic">
						                <table class="display" id="pos-table">
						                    <thead>
						                        <tr id="datatable_header">
						                   			<th>ID</th>
						                            <th width="44%">დასახელება</th>
						                            <th width="20%">კოდი</th>
						                            <th width="12%">ფასი</th>
						                            <th width="12%">რაოდ.</th>
						                            <th width="12%">ჯამი</th>
						                            <th class="check"></th>
						                        </tr>
						                    </thead>
						                </table>
						            </div>
						    	</div>
						    </div>
						</div>
						<div class="fg-toolbar ui-toolbar ui-widget-header ui-corner-bl ui-corner-br ui-helper-clearfix" id="pos-table-footer"></div>
					</td>
					<td width="8">&nbsp;</td>
					<td>
						<div id="sale_details">
							<table id="sales_items">
								<tr>
									<td class="left">პროდუქტების რაოდენობა:</td>
									<td class="right"><span id="prod-quantity">0</span></td>
								</tr>
							</table>
							<table id="sales_items_total">
								<tr>
									<td class="left">ჯამი:</td>
									<td class="right"><span id="sum-price">0.00</span> GEL</td>
								</tr>
							</table>
						</div>
						<div id="make_payment">
							<div id="pay_box">
								<input type="text" value="0.00" id="sum-total-price" size="10">
							</div>
							<div id="add_payment_button">
								<button id="add_payment">გადახდა</button>
							</div>
						</div>
					</td>
				</tr>
			</table>
		</div>
	</div>
</body>
</html>